package com.sks.utils;

import java.awt.image.BufferedImage;
import java.util.Properties;
import java.util.Random;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

public class ValidateCodeUtil {
	static DefaultKaptcha defaultKaptcha = new DefaultKaptcha();

	static {
	    defaultKaptcha.setConfig(new Config(new Properties()));
	}
	
	public static BufferedImage getBuggeredImage(String randCodeText){
		return defaultKaptcha.createImage(randCodeText);
	}

	public static String getRandomStr(int length){
		String text="";
		Random random = new Random();   
	    for(int i = 0; i < length; i++) {   
	    String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字   
	        if("char".equalsIgnoreCase(charOrNum)){ // 字符串   
	            int charStr = random.nextInt(2) % 2 == 0 ? 65 : 97; //取得大写字母还是小写字母   
	            text += (char) (charStr + random.nextInt(26));   
	        }else if("num".equalsIgnoreCase(charOrNum)) {  // 数字   
	        	text += String.valueOf(random.nextInt(10));   
	        } 
	     } 

		//System.out.println(text);
		return text;
	}
	
}
